package com.bytedance.scenedemo.animatiom;

import com.bytedance.scene.dialog.bottomsheet.ValueAnimator;
import com.bytedance.scene.group.GroupScene;
import com.bytedance.scene.navigation.NavigationSceneGetter;
import com.bytedance.scenedemo.ResourceTable;
import ohos.aafwk.content.Intent;
import ohos.agp.animation.Animator;
import ohos.agp.animation.AnimatorGroup;
import ohos.agp.animation.AnimatorProperty;
import ohos.agp.animation.AnimatorValue;
import ohos.agp.components.*;

/**
 * Created by JiangQi on 8/29/18.
 */
public class TransitionDemo extends GroupScene {
    private boolean isAutoPlayAnimation = false;
    private ComponentContainer componentContainer;
    private Image imageAmplification;


    @Override
    public ComponentContainer onCreateView(LayoutScatter inflater, ComponentContainer container, Intent savedInstanceState) {
        componentContainer = (ComponentContainer) inflater.parse(ResourceTable.Layout_transtion_demo0, container, false);
        isAutoPlayAnimation = false;
        initView();
        return componentContainer;
    }


    private void startAnimation() {
        int height = getAbility().getWindow().getLayoutConfig().get().height;
        int width = getAbility().getWindow().getLayoutConfig().get().width;
        System.out.println(">>>>>>>>>>>" + btn.getContentPositionY());
        AnimatorProperty btnAnimatorProperty1 = btn.createAnimatorProperty();
        btn.getComponentTreeObserver().addWindowBoundListener(new ComponentTreeObserver.WindowBoundListener() {
            @Override
            public void onWindowBound() {
                btnAnimatorProperty1
                        .moveFromY((float) (height - btn.getHeight()) / 2)
                        .moveToY(btn.getContentPositionY())
                        .moveFromX((float) (width - btn.getWidth()) / 2)
                        .moveToX(btn.getContentPositionX())
                        .setDuration(2000);
            }

            @Override
            public void onWindowUnbound() {

            }
        });
        ValueAnimator animatorValueBtn = ValueAnimator.ofFloat();
        animatorValueBtn.setValueUpdateListener((animatorValue, v) -> btn.setWidth((int) (btn.getWidth() - 1)));
        animatorValueBtn.setDuration(2000);

        AnimatorProperty btnAnimatorProperty2 = btn.createAnimatorProperty();
        btnAnimatorProperty2.rotate(0).setDuration(1000);
        animatorGroup = new AnimatorGroup();
        AnimatorGroup.Builder animatorGroupBuilder = animatorGroup.build();
        animatorGroup.setStateChangedListener(new Animator.StateChangedListener() {
            @Override
            public void onStart(Animator animator) {
                btn.setText("打开 SCENE");
                btn.setRotation(20);
                btn.setWidth((int) (btn.getWidth() + btn.getWidth() * 0.5));
            }

            @Override
            public void onStop(Animator animator) {

            }

            @Override
            public void onCancel(Animator animator) {

            }

            @Override
            public void onEnd(Animator animator) {
                btn.setWidth(100 * 3);
            }

            @Override
            public void onPause(Animator animator) {

            }

            @Override
            public void onResume(Animator animator) {

            }
        });
        animatorGroupBuilder.addAnimators(btnAnimatorProperty1, animatorValueBtn).addAnimators(btnAnimatorProperty2);


        Button btnNothingDoWithRight = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_nothing_do_with_right);
        animatorPropertyRight = btnNothingDoWithRight.createAnimatorProperty();
        animatorPropertyRight.moveFromY(height).moveToY(0).setDuration(2000);

        btnNothingDoWithLeft = (Button) componentContainer.findComponentById(ResourceTable.Id_btn_nothing_do_with_left);
        animatorPropertyLeft = btnNothingDoWithLeft.createAnimatorProperty();
        animatorPropertyLeft.moveFromY(0).moveToY(height).setDuration(2000);


        imageBlue2 = (Image) componentContainer.findComponentById(ResourceTable.Id_image_red);
        imageBlue2.setVisibility(Component.VISIBLE);
        imageAmplification.setVisibility(Component.VISIBLE);


        slAmplification = (StackLayout) componentContainer.findComponentById(ResourceTable.Id_sl_amplification);
        float scale = (float) imageAmplification.getWidth() / (float) (slAmplification.getWidth());
        AnimatorProperty imageAnimatorProperty1 = imageBlue2.createAnimatorProperty();
        imageAnimatorProperty1
                .setTarget(imageBlue2)
                .scaleX(scale).scaleY(scale)
                .setDuration(2000);
        AnimatorProperty imageAnimatorProperty2 = imageBlue2.createAnimatorProperty();
        animatorGroupImageAmplification = new AnimatorGroup();
        animatorGroupImageAmplification.setStateChangedListener(new StateChangedListenerAdapter() {
            @Override
            public void onEnd(Animator animator) {
                imageBlue2.setRotation(0);
                imageBlue2.setVisibility(Component.HIDE);
                imageAmplification.setVisibility(Component.VISIBLE);
                componentContainer.postLayout();
            }
        });

        slAmplification.getComponentTreeObserver().addWindowBoundListener(new ComponentTreeObserver.WindowBoundListener() {
            @Override
            public void onWindowBound() {
                imageAnimatorProperty2
                        .moveToY(slAmplification.getContentPositionY())
                        .moveToX(slAmplification.getContentPositionX() + 180)
                        .rotate(45)
                        .setDuration(2000);
                animatorGroupImageAmplification.runSerially(imageAnimatorProperty1, imageAnimatorProperty2);
            }

            @Override
            public void onWindowUnbound() {

            }
        });
    }

    public class StateChangedListenerAdapter implements Animator.StateChangedListener {

        @Override
        public void onStart(Animator animator) {

        }

        @Override
        public void onStop(Animator animator) {

        }

        @Override
        public void onCancel(Animator animator) {

        }

        @Override
        public void onEnd(Animator animator) {

        }

        @Override
        public void onPause(Animator animator) {

        }

        @Override
        public void onResume(Animator animator) {

        }
    }

    private Button btn;
    Button btnNothingDoWithLeft;
    private StackLayout slAmplification;
    Image imageBlue2;

    private void initView() {
        btn = (Button) componentContainer.findComponentById(ResourceTable.Id_btn);
        imageAmplification = (Image) componentContainer.findComponentById(ResourceTable.Id_image_amplification);
        btn.setClickedListener(component -> {
            System.out.println("=============getContentPositionX=" + imageBlue2.getContentPositionX());
            System.out.println("=============getContentPositionY=" + imageBlue2.getContentPositionY());
            System.out.println("=============getPivotX=" + imageBlue2.getPivotX());
            System.out.println("=============getPivotY=" + imageBlue2.getPivotY());
            if (imageBlue2cpX == 0) {
                imageBlue2cpX = imageBlue2.getContentPositionX();
                imageBlue2cpY = imageBlue2.getContentPositionY();
                imageBlue2PivotX = imageBlue2.getPivotX();
                imageBlue2PivotY = imageBlue2.getPivotY();
            } else {
                imageBlue2.setContentPosition(imageBlue2cpX, imageBlue2cpY);
                float scale = (float) slAmplification.getWidth() / (float) (imageBlue2.getWidth());
                imageBlue2.setScale(scale, scale);
                imageBlue2.setVisibility(Component.VISIBLE);
            }
            NavigationSceneGetter.requireNavigationScene(this).push(TransitionDemo1.class);
            isAutoPlayAnimation = true;
        });
        startAnimation();
    }

    private float imageBlue2cpX = 0f;
    private float imageBlue2cpY = 0f;
    private float imageBlue2PivotX = 0f;
    private float imageBlue2PivotY = 0f;

    AnimatorGroup animatorGroup;
    AnimatorProperty animatorPropertyRight;
    AnimatorProperty animatorPropertyLeft;
    AnimatorGroup animatorGroupImageAmplification;

    @Override
    protected void onPostResume() {
        super.onPostResume();
        if (isAutoPlayAnimation) {
            imageBlue2.setPivot(0, 0);
            btnNothingDoWithLeft.setVisibility(Component.VISIBLE);
            imageAmplification.setVisibility(Component.HIDE);
            imageBlue2.setVisibility(Component.VISIBLE);
            animatorGroup.start();
            animatorPropertyRight.start();
            animatorPropertyLeft.start();
            animatorGroupImageAmplification.start();
            animatorGroupImageAmplification.release();
            isAutoPlayAnimation = false;
        } else {
            imageBlue2.setVisibility(Component.INVISIBLE);
        }
    }
}
